﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Exam.Core.DBUtility;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace Exam.Core.SQLDAL
{
    public class Master {

        public Exam.Core.Model.Master getMaster(int masterId) {
            string SQL = "SELECT top 1 * FROM Master WHERE MasterID=" + masterId;
            DataTable dt = DbHelperSQL.Query(SQL).Tables[0];
            Exam.Core.Model.Master model = new Exam.Core.Model.Master();
            if(dt.Rows.Count==1){
                model.MasterID = dt.Rows[0]["MasterID"].ToString().Trim();
                model.MasterName = dt.Rows[0]["MasterName"].ToString().Trim();
                model.MasterPassword = dt.Rows[0]["MasterPassword"].ToString().Trim();
                model.DepartmentID = dt.Rows[0]["DepartmentID"].ToString().Trim();
                model.CourseId = dt.Rows[0]["CourseId"].ToString().Trim();
                //model.DeptList = new Department().getDepartmentAllList(model.DepartmentID.Trim(','));
                //model.CourseList = new Course().getCoursesList(model.CourseId.Trim(','));
            }
            return model;
        }

        public Exam.Core.Model.Master getMasterByName(string uname) {
            string SQL = "SELECT top 1 * FROM Master WHERE MasterName=@MasterName";
            SqlParameter[] parameters = {
					new SqlParameter("@MasterName", SqlDbType.VarChar,100)};
            parameters[0].Value = uname;
            Exam.Core.Model.Master obj = new Exam.Core.Model.Master();
            using (SqlDataReader dr = DbHelperSQL.ExecuteReader(SQL, parameters)) {
                while (dr.Read()) {
                    obj.MasterID = dr.GetInt32(0).ToString().Trim();
                    obj.MasterName = dr.GetString(1).Trim();
                    obj.MasterPassword = dr.GetString(2).Trim();
                    obj.DepartmentID = dr.GetString(3).Trim();
                    obj.CourseId = dr.GetValue(4).ToString().Trim();
                }
            }
            return obj;

            //DataTable dt = DbHelperSQL.Query(SQL).Tables[0];
            //Exam.Core.Model.Master model = new Exam.Core.Model.Master();
            //if (dt.Rows.Count == 1) {
            //    model.MasterID = dt.Rows[0]["MasterID"].ToString().Trim();
            //    model.MasterName = dt.Rows[0]["MasterName"].ToString().Trim();
            //    model.MasterPassword = dt.Rows[0]["MasterPassword"].ToString().Trim();
            //    model.DepartmentID = dt.Rows[0]["DepartmentID"].ToString().Trim();
            //    model.CourseId = dt.Rows[0]["CourseId"].ToString().Trim();
            //    //model.DeptList = new Department().getDepartmentAllList(model.DepartmentID.Trim(','));
            //    //model.CourseList = new Course().getCoursesList(model.CourseId.Trim(','));
            //}
            //return model;
        }


    }
}
